import "package:flutter/material.dart";
import "package:myapp/_common/SliverHeaderDelegate.dart";

class LiveListTabWidget extends StatefulWidget {
  final List<String> tabList;
  final int initActive;
  final Function? onChange;
  const LiveListTabWidget({
    super.key,
    required this.tabList,
    this.initActive = 0,
    this.onChange,
  });

  @override
  State<StatefulWidget> createState() {
    return _LiveListTabWidgetState();
  }
}

class _LiveListTabWidgetState extends State<LiveListTabWidget> {
  late int tabActive;
  @override
  void initState() {
    tabActive = widget.initActive;
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return SliverPersistentHeader(
      floating: true,
      delegate: SliverHeaderDelegate.fixedHeight(
        height: 36,
        child: Container(
          height: 36,
          decoration: const BoxDecoration(
            color: Color.fromARGB(255, 244, 244, 244),
          ),
          margin: const EdgeInsets.only(right: 6),
          child: ListView.builder(
            itemCount: widget.tabList.length,
            scrollDirection: Axis.horizontal,
            itemBuilder: (context, index) {
              return GestureDetector(
                onTap: () {
                  setState(() {
                    tabActive = index;
                    if (widget.onChange != null) widget.onChange!(index);
                  });
                },
                child: Padding(
                  padding: const EdgeInsets.fromLTRB(10, 0, 10, 0),
                  child: Row(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      Text(
                        widget.tabList[index],
                        style: tabActive == index
                            ? const TextStyle(
                                fontWeight: FontWeight.bold,
                              )
                            : const TextStyle(
                                color: Color.fromARGB(255, 116, 116, 116),
                              ),
                      ),
                    ],
                  ),
                ),
              );
            },
          ),
        ),
      ),
    );
  }
}

